package com.ruoyi.web.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 用于配置交换机和队列对应关系
 * 新增消息队列应该按照如下步骤
 * 1、增加queue bean，参见queueXXXX方法
 * 2、增加queue和exchange的binding
 * @author chenhf
 * @create 2017-10-23 上午10:33
 **/
@Configuration
@AutoConfigureAfter(RabbitConfig.class)
public class RabbitMqExchangeConfig {

    /** logger */
    private static final Logger logger = LoggerFactory.getLogger(RabbitMqExchangeConfig.class);

    /**
     * @Author:chenhf
     * @Description: 主题型交换机
     * @Date:下午5:49 2017/10/23
     * @param
     * @return
     */
    @Bean
    TopicExchange contractTopicExchangeDurable(RabbitAdmin rabbitAdmin){
        TopicExchange contractTopicExchange = new TopicExchange(RabbitMqEnum.Exchange.CONTRACT_TOPIC.getCode());
        rabbitAdmin.declareExchange(contractTopicExchange);
        logger.debug("完成主题型交换机bean实例化");
        return contractTopicExchange;
    }
    /**
     * 直连型交换机
     */
    @Bean
    DirectExchange contractDirectExchange(RabbitAdmin rabbitAdmin) {
        DirectExchange contractDirectExchange = new DirectExchange(RabbitMqEnum.Exchange.CONTRACT_DIRECT.getCode(),true,false);
        rabbitAdmin.declareExchange(contractDirectExchange);
        logger.debug("完成直连型交换机bean实例化");
        return contractDirectExchange;
    }

    //在此可以定义队列

    //topic 1
    @Bean
    Queue queueTopicWarn(RabbitAdmin rabbitAdmin){
        Queue queue = new Queue(RabbitMqEnum.QueueName.ORDERQUEUE.getCode());
        rabbitAdmin.declareQueue(queue);
        logger.debug("监测预警队列1实例化完成");
        return queue;
    }

    //topic 2
    //在此处完成队列和交换机绑定
    @Bean
    Binding bindingDirectChange(Queue queueWarn, DirectExchange exchange, RabbitAdmin rabbitAdmin){
        Binding binding = BindingBuilder.bind(queueWarn).to(exchange).with(RabbitMqEnum.QueueEnum.QUE_ORDER_KEY.getCode());
        rabbitAdmin.declareBinding(binding);
        logger.debug("检测预警与直连型交换机绑定完成");
        return binding;
    }

    //topic binding1
    @Bean
    Binding bindingQueueTopicDanger(Queue queueDanger, DirectExchange exchange, RabbitAdmin rabbitAdmin){
        Binding binding = BindingBuilder.bind(queueDanger).to(exchange).with(RabbitMqEnum.QueueEnum.QUE_DATA_KEYTOP.getCode());
        rabbitAdmin.declareBinding(binding);
        logger.debug("风险分布与话题交换机1绑定完成");
        return binding;
    }
}
